from random import randrange
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from two.models import Student, XueSheng, Grade


# Create your views here.
def two_index(request):
    return render(request, '')


def add_student(request):
    student = Student()
    student.name = f'奥特曼{randrange(999)}'
    student.age = randrange(999)
    student.save()
    return HttpResponse(f'添加{student.name}成功')


def get_students(request):
    students = Student.objects.all()
    for student in students:
        print(student.name)
    return HttpResponse('Student List')


def show_students(request):
    students = Student.objects.all()
    context = {
        'data': '666',
        'students': students,
    }
    return render(request, 'student_list.html', context=context)


def update_student(request):
    student = Student.objects.get(pk=2)
    student.name = '修改了名字'
    student.save()
    return HttpResponse('ok')


def delete_student(request):
    student = Student.objects.get(pk=3)
    student.delete()
    return HttpResponse('delete ok 啦')


def index2(request):
    index_two = loader.get_template('index3.html')
    result = index_two.render()
    return result


def get_grade(request):
    xs = XueSheng.objects.get(pk=1)
    grade = xs.grade
    return HttpResponse(f'xs 的 grade {grade}')


def get_xuesheng(request):
    grade = Grade.objects.get(pk=2)
    xses = grade.xuesheng_set.all()
    context = {
        'grade': grade,
        'xses': xses
    }
    return render(request, 'xuesheng.html', context=context)